//创建首页
$("#menuSources").append("<li id='indexpage' class='active'> <a href='#'><i class='zmdi zmdi-home'></i> 首页</a></li>");

//根据点选的菜单修改样式，并打开iframe页面
var selectedWedgit=$('#indexpage');
selectedWedgit.attr("class","active");
var url, id;
function goPage(url, id) {
	
	hideMenu();//隐藏菜单
	
    if (url != null && url != "") {
        $('iframe').attr("src", url + "");
    }
    $('#' + id).attr("class", "active");
    selectedWedgit.attr("class", "");
    var indexUl = "#second" + id;
    selectedWedgit = $(indexUl);
}

//菜单初始化
$.ajax({
    type: "POST",
    url: "/platform/windows/menu/showMenuTree",
    dataType: "json",
    async: false,
    success: function (data) {

        if (data.message == "ok") {
            //获取经过权限验证后的菜单json
            var json = data.menus;
            //对菜单树进行排列
            //第一步：取出根节点下的菜单,形成一级菜单
            var countRootMenu = 0;
            for (var i = 0; i < json.length; i++) {
                if (json[i]["parent_menu"] == null) {
                    $("#menuSources").append("<li id='first" + json[i]["id"] + "'> <a href='#'><i class='zmdi zmdi-menu'></i> <span>" + json[i]["name"] + "</span></a></li>");
                    countRootMenu++;
                }
            }
            //判断是否存在分配有二级菜单的一级菜单
            if (countRootMenu > 0) {
                for (var j = 0; j < json.length; j++) {
                    if (json[j]["parent_menu"] != null) {
                        if ($("#first" + json[j]["parent_menu"]).has("ul").length == 0) {
                            $("#first" + json[j]["parent_menu"]).attr("class", "sub-menu");
                            $("#first" + json[j]["parent_menu"]).append("<ul id='second" + json[j]["parent_menu"] + "'></ul>");
                        }
                        $("#second" + json[j]["parent_menu"]).append("<li id='" + json[j]["id"] + "'><a href='javascript:goPage(\"" + json[j]["url"] + "\"," + json[j]["id"] + ");'>" + json[j]["name"] + "</a></li>");
                    }
                }
            }
        }
        else {

        }
    }
});

//隐藏菜单
function hideMenu()
{
	$('#tw-switch').prop('checked', false);
	setTimeout(function () {
        $('body').removeClass('toggled sw-toggled');
        localStorage.setItem('ma-layout-status', 0);
    }, 250);
	iconCtrlMenu();
}

//显示菜单
function showMenu()
{
	$('#tw-switch').prop('checked', true);
	setTimeout(function () {
        $('body').addClass('toggled sw-toggled');
        localStorage.setItem('ma-layout-status', 1);
    }, 250);
	iconCtrlMenu();
}

//左侧菜单联动控制
function iconCtrlMenu()
{
	var selected=$('#menu-trigger');
    $('.sub-menu.toggled').not('.active').each(function () {
    	selected.removeClass('toggled');
    	selected.find('ul').hide();
    });
    $('.profile-menu .main-menu').hide();
    setTimeout(function () {
        $('#sidebar').removeClass('toggled');
        $('#header').removeClass('sidebar-toggled');
        $('#menu-trigger').removeClass('open');
    });
}
